package cz.cvut.manatee.client.concordance;

import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

import cz.cvut.manatee.client.IntegerTextBox;
import cz.cvut.manatee.client.Resources;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.concordance.CollectedParameters;
import cz.cvut.manatee.shared.concordance.CollectedSampleParameters;

public class SampleWidget extends ConcordanceFormWidget {
	
	private final HorizontalPanel samplePanel;
	private final Label sampleTitle; 
	private final IntegerTextBox sampleInput;
	
	public SampleWidget() {
		
		samplePanel = new HorizontalPanel();
		samplePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		sampleTitle = new Label("Počet výsledků v náhodné redukci:");
		samplePanel.add(sampleTitle);
		
		sampleInput = new IntegerTextBox(30);
		samplePanel.add(sampleInput);
		
		initWidget(samplePanel);
	}
	
	public boolean validate() {
		this.clearError();
		
		boolean validation = true;
		
		int sampleValue = this.sampleInput.getIntegerValue();
		if( sampleValue <= 0 ) {
			sampleInput.addStyleName(Resources.INSTANCE.css().formError());
			validation = false;
		} 
		
		return validation;
	}

	public void setAttributes(AttributItem[] attributs) {
		
	}
	
	public void clearError(){
		sampleInput.removeStyleName(Resources.INSTANCE.css().formError());
	}
	
	public int getSampleSize(){
		return new Integer(sampleInput.getValue()).intValue();
	}

	protected void setSampleSize(int sampleSize) {
		sampleInput.setValue(""+sampleSize);
	}
	
	public CollectedParameters getParameters() {
		return new CollectedSampleParameters(getSampleSize());
	}

	public void setParameters(CollectedParameters params) {
		setSampleSize(((CollectedSampleParameters)params).getSampleCount());
	}

}
